package com.example.smartpark.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.smartpark.common.Result;
import com.example.smartpark.dto.ReservationDTO;
import com.example.smartpark.dto.ReservationQueryDTO;
import com.example.smartpark.service.ReservationService;
import com.example.smartpark.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 预约控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/reservations")
public class ReservationController {

    @Autowired
    private ReservationService reservationService;

    /**
     * 获取预约统计信息
     */
    @GetMapping("/statistics")
    public Result<?> getReservationStatistics() {
        log.info("获取预约统计信息");
        // TODO: 实现预约统计逻辑
        return Result.success();
    }

    /**
     * 获取支付方式列表
     */
    @GetMapping("/payment-methods")
    public Result<?> getPaymentMethods() {
        log.info("获取支付方式列表");
        // TODO: 实现获取支付方式列表逻辑
        return Result.success();
    }

    /**
     * 分页查询预约
     *
     * @param queryDTO 查询条件
     * @param token JWT令牌
     * @return 预约分页结果
     */
    @GetMapping
    public Result<IPage<ReservationDTO>> getReservationPage(
            ReservationQueryDTO queryDTO,
            @RequestHeader("Authorization") String token) {
        log.info("分页查询预约: queryDTO={}", queryDTO);
        
        // 从token中获取用户ID
        Integer userIdInt = JwtUtils.getUserIdFromToken(token.replace("Bearer ", ""));
        
        // 如果不是管理员查询，则添加用户ID限制
        if (!Boolean.TRUE.equals(queryDTO.getIsAdmin())) {
            queryDTO.setUserId(userIdInt);
        }
        
        return Result.success(reservationService.getReservationPage(queryDTO));
    }

    /**
     * 查询预约详情
     *
     * @param id 预约ID
     * @return 预约详情
     */
    @GetMapping("/{id}")
    public Result<ReservationDTO> getReservationById(@PathVariable Long id) {
        log.info("查询预约详情: id={}", id);
        return Result.success(reservationService.getReservationById(id));
    }

    /**
     * 创建预约
     *
     * @param reservationDTO 预约DTO
     * @param token JWT令牌
     * @return 创建的预约
     */
    @PostMapping
    public Result<ReservationDTO> createReservation(
            @RequestBody @Valid ReservationDTO reservationDTO,
            @RequestHeader("Authorization") String token) {
        log.info("创建预约: reservationDTO={}", reservationDTO);
        
        // 从token中获取用户ID
        Integer userIdInt = JwtUtils.getUserIdFromToken(token.replace("Bearer ", ""));
        reservationDTO.setUserId(userIdInt);
        
        return Result.success(reservationService.createReservation(reservationDTO));
    }

    /**
     * 取消预约
     *
     * @param id 预约ID
     * @param remark 取消原因
     * @return 取消后的预约
     */
    @PutMapping("/{id}/cancel")
    public Result<ReservationDTO> cancelReservation(
            @PathVariable Long id,
            @RequestParam(required = false) String remark) {
        log.info("取消预约: id={}, remark={}", id, remark);
        return Result.success(reservationService.cancelReservation(id, remark));
    }

    /**
     * 管理员确认预约
     *
     * @param id 预约ID
     * @return 确认后的预约
     */
    @PutMapping("/{id}/confirm")
    public Result<ReservationDTO> confirmReservation(@PathVariable Long id) {
        log.info("确认预约: id={}", id);
        return Result.success(reservationService.confirmReservation(id));
    }

    /**
     * 管理员拒绝预约
     *
     * @param id 预约ID
     * @param remark 拒绝原因
     * @return 拒绝后的预约
     */
    @PutMapping("/{id}/reject")
    public Result<ReservationDTO> rejectReservation(
            @PathVariable Long id,
            @RequestParam(required = false) String remark) {
        log.info("拒绝预约: id={}, remark={}", id, remark);
        return Result.success(reservationService.rejectReservation(id, remark));
    }
} 